package com.atshiyou.util;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MyFilter implements Filter {

    private List<String> sensitive = new ArrayList<String>();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //加载敏感词文件
        ServletContext servletContext = filterConfig.getServletContext();
        String realPath = servletContext.getRealPath("/WEB-INF/classes/sensitive");
        //读取文件
        BufferedReader br  = null;
        try {
           br  = new BufferedReader(new FileReader(realPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            while(br.readLine()!=null){
                sensitive.add(br.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("敏感词的初始化信息:"+sensitive);
        System.out.println("过滤器初始化完成");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("filter开始执行");

        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        request.setCharacterEncoding("utf-8");//处理所有的servlet乱码
        response.setContentType("text/html;charset=utf-8");

        String uname = (String) request.getSession().getAttribute("username");
        String requestURI = request.getRequestURI();// 获得request的请求地址
        System.out.println("request的请求地址:"+requestURI);
        //对某些资源放行:用户未登录情况下可以访问的资源,或者session有值的情况下
        if(requestURI.endsWith("/") || requestURI.endsWith("/message.jsp") || requestURI.endsWith("/index.jsp")
                || requestURI.endsWith("regist")|| requestURI.endsWith("login")
                ||requestURI.endsWith("/regist.html")|| uname !=null){
            System.out.println("过滤器放行一次-----");
            //获取用户输入的评论信息
            String usercomment = request.getParameter("Usercomment");
            System.out.println("----@@@用户的评论是---:"+usercomment);
            if(usercomment!=null){
                //当用户评论信息输入的时候我们对敏感词进行过滤
                for (String str : sensitive){
                    if(usercomment.contains(str)){
                        usercomment = usercomment.replaceAll(str,"***");
                    }
                }
            }
            request.getSession().setAttribute("handledComment",usercomment);
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            response.sendRedirect("/message.jsp");
            return;
        }
        System.out.println("filter结束执行");
    }

    @Override
    public void destroy() {
        System.out.println("过滤器已销毁");

    }
}
